<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
import axios from "axios";
import { mapActions } from "vuex";
import tools from "@/utils/tools";
export default {
  created() {
    // 如果通过地址栏获取token,采用模拟登录方式
    let accessToken = tools.getQueryString("token");
    if (accessToken) {
      this.$store.commit("setToken", accessToken);
    }
    if (this.$store.state.user.token && !this.$store.state.user.getBaseInfo) {
      accessToken && this.$storage.setLoginSource(1);
      this.getUserData();
      this.getAmountData();
    }
  },
  mounted(){
    this.getVersion();
    setInterval(() => {
      this.getVersion();
    }, 5000);
  },
  methods: {
    ...mapActions(["getUserData", "getAmountData"]),
        getVersion() {
      let url = `//${window.location.host}/version.json`;
      if (process.env.NODE_ENV === 'production') {
        axios.get(url).then((res) => {
          let lastVersion = res.data.version;
          if (localStorage.version == undefined) {
            localStorage.setItem('version', lastVersion);
          } else {
            if (localStorage.version != lastVersion) {
              localStorage.removeItem('version');
              this.$Modal.confirm({
                  title:'提示',
                  content: "<p>检测到版本更新，刷新页面</p>",
                  onOk: () => {
                   window.location.reload();
                  },
                });
            }
          }
        });
      }
    }
  },
};
</script>

<style lang="scss">
.ivu-checkbox {
  margin-right: 10px;
}
</style>
